/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.marcelo.agro.entidades.validadores;

import java.awt.Color;
import javax.swing.JComponent;
import org.jdesktop.swingx.JXDatePicker;

/**
 *
 * @author marcelo
 */
public abstract class AbstractValidator implements Validadores{
    private JComponent c;
    private Color oldBackground;
    private Color errorBackgroud;
    private Color DEFAULT_ERROR_COLOR = Color.pink;
    private String DEFAULT_MENSAJE_ERROR = "No paso la validación";
    private String mensaje;
    
    public AbstractValidator(JComponent c, Color errorBackgroud) {
        this.c = c;
        this.oldBackground = c.getBackground();
        this.errorBackgroud = errorBackgroud;
    }
    
    public AbstractValidator(JComponent c) {
        if (c instanceof JXDatePicker) {
            this.c = ((JXDatePicker) c).getEditor();
            this.oldBackground = this.c.getBackground();
        }
        else{
            this.c = c;
            this.oldBackground = c.getBackground();
        }    
        this.errorBackgroud = DEFAULT_ERROR_COLOR;
    }
    
    
    @Override
    public boolean checkValidity(){
        boolean result = isValid();
        if (!result) {
            if (!c.getBackground().equals(errorBackgroud))
                c.setBackground(errorBackgroud);
        }
        else {
            if (c.getBackground().equals(errorBackgroud))
                c.setBackground(oldBackground);
        }
        return result;
    }
    
    public abstract boolean isValid();
    
}
